package com.njit;

import javax.swing.*;
import java.awt.*;
import java.sql.PreparedStatement;

import db.db;

/**
 * Created by ospip on 2017/11/30.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class personadd extends JFrame {
    private JTextField userid, username;
    private JRadioButton sexfemale, sexmale;
    private JPanel sex, birth;
    private JTextField year, dept;
    private JComboBox month, day, prof;
    private JButton ok, cancel;

    public personadd() {
        super();
        this.setSize(350, 300);
        this.setTitle("Login");
        this.setLocationRelativeTo(getOwner());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(new GridLayout(7, 2));
        // Begin
        this.add(new JLabel("员工号"));
        userid = new JTextField(10);
        this.add(userid);
        this.add(new JLabel("姓名"));
        username = new JTextField(10);
        this.add(username);
        this.add(new JLabel("性别"));
        sexmale = new JRadioButton("男", true);
        sexfemale = new JRadioButton("女");
        ButtonGroup bg = new ButtonGroup();
        bg.add(sexmale); bg.add(sexfemale);
        sex = new JPanel(new GridLayout(1, 2));
        sex.add(sexmale); sex.add(sexfemale);
        this.add(sex);
        this.add(new JLabel("出生日期"));
        year = new JTextField(4);
        month = new JComboBox();
        for (int i = 1; i < 12; i++) {
            month.addItem(i);
        }
        day = new JComboBox();
        for (int i = 1; i < 31; i++) {
            day.addItem(i);
        }
        birth = new JPanel();
        birth.add(year);
        birth.add(new JLabel("-"));
        birth.add(month);
        birth.add(new JLabel("-"));
        birth.add(day);
        this.add(birth);
        this.add(new JLabel("职称"));
        prof = new JComboBox();
        prof.addItem("初级");
        prof.addItem("中级");
        prof.addItem("高级");
        this.add(prof);
        this.add(new JLabel("部门"));
        dept = new JTextField(10);
        this.add(dept);
        ok = new JButton("Add");
        cancel = new JButton("Cancel");
        this.add(ok);
        this.add(cancel);
        ok.addActionListener((e -> {
            db dbcon = new db();
            try {
                String sql = "insert into person values(?,?,?,?,?,?)";
                PreparedStatement prestate = dbcon.PreparedStatement(sql);
                prestate.setString(1, userid.getText());
                prestate.setString(2, username.getText());
                prestate.setString(3, sexmale.isSelected()?sexmale.getText():sexfemale.getText());
                prestate.setString(4, year.getText() + "-" + month.getSelectedItem() + "-" + day.getSelectedItem());
                prestate.setString(5, prof.getSelectedItem().toString());
                prestate.setString(6, dept.getText());
                prestate.execute();
                JOptionPane.showMessageDialog(null, "Succeed");
                dispose();
                personselect psel = new personselect();
                psel.setVisible(true);
            }
            catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }));
        cancel.addActionListener((e -> {
            dispose();
        }));
        // End
    }
    public static void main(String[] args) {
        new personadd().setVisible(true);
    }
}
